/*
 *  rigidtetra.cpp
 *  EPPI-0.0
 *
 *  Created by Juan Carlos Garcia Orden on 01/04/11
 *
 */

#include "rigidtetra.h"

RigidTetra::RigidTetra(ifstream &in, string cname, MecSystem *pSys, vector<string>& cvnames)
:	Joint(in,cname,pSys)
{	
	dim = parentSystem->dim;	/** Must be set always.  dim belongs to the mother class Joint */
	
	vector<string> auxsv(2);
	vector<string> vnames = cvnames;
	int i;
	
	for (i=0; i<4; ++i)
	{
		if(checkForSlash(vnames[i])==false)
			addSystemName(vnames[i],parentSystem->name);
	}
	
	// First couple
	auxsv[0] = vnames[0];
	auxsv[1] = vnames[1];
//	vComponents.push_back(new Consdis(auxsv,dim));
	vConstraints.push_back(new Consdis(auxsv,dim));
	
	// Second couple
	auxsv[0] = vnames[0];
	auxsv[1] = vnames[2];
//	vComponents.push_back(new Consdis(auxsv,dim));
	vConstraints.push_back(new Consdis(auxsv,dim));
	
	// Third couple
	auxsv[0] = vnames[0];
	auxsv[1] = vnames[3];
//	vComponents.push_back(new Consdis(auxsv,dim));
	vConstraints.push_back(new Consdis(auxsv,dim));
	
	// Fourth couple
	auxsv[0] = vnames[1];
	auxsv[1] = vnames[2];
//	vComponents.push_back(new Consdis(auxsv,dim));
	vConstraints.push_back(new Consdis(auxsv,dim));
	
	// Fifth couple
	auxsv[0] = vnames[1];
	auxsv[1] = vnames[3];
//	vComponents.push_back(new Consdis(auxsv,dim));
	vConstraints.push_back(new Consdis(auxsv,dim));
	
	// Sixth couple
	auxsv[0] = vnames[2];
	auxsv[1] = vnames[3];
//	vComponents.push_back(new Consdis(auxsv,dim));
	vConstraints.push_back(new Consdis(auxsv,dim));
	
	//cout << "JKJKJKJK " << endl;
	//printSTLVector(vnames);
}

// ***
/*
void RigidTetra::setPTPointsComp()
{
	int j, k;
	vector<string> auxs;
	map<string, Point*, less<string> > maux;
	pair<string,Point*> mauxEntry;
	pair<string,int> sharedEntry;
	
	for(j=0; j<vComponents.size(); j++)		
	//Loop on the components
	{
		auxs = (vComponents[j]->getPointNames());
		for(k=0; k<auxs.size(); k++)		
		// Loop on the names of the points within each component
		{
			// If the point it is not at the local process ...
			if( (parentSystem->checkIfPointProcess(auxs[k]))==false )
			{
				// ...  create it at root system, copying the original point from the remote process
				parentSystem->createPointAtRoot( parentSystem->getPTPoint(auxs[k]) );
				// ...  and add the name of the shared point at the remote process
//				cout << "Nombre del sistema = " << parentSystem->name << endl;
//				cout << "en el proceso = " << parentSystem->getProcNum() << endl;
//				cout << "Nombre del punto = " << auxs[k] << endl;
				parentSystem->addRemoteSharedPoint(auxs[k],parentSystem->getProcNum());
			}
			mauxEntry.first = auxs[k];
			mauxEntry.second = (parentSystem->getPTPointLocal(auxs[k]));
			maux.insert(mauxEntry);
		}
		vComponents[j]->setMPoints(maux);
		maux.clear();
	}
}

// ***

void RigidTetra::setPTPointsLoad()
{
	int j, k;
	vector<string> auxs;
	map<string, Point*, less<string> > maux;
	pair<string,Point*> mauxEntry;
	pair<string,int> sharedEntry;
	
	for(j=0; j<vLoads.size(); j++)		
	//Loop on the Loads
	{
		auxs = (vLoads[j]->getPointNames());
		for(k=0; k<auxs.size(); k++)		
		// Loop on the names of the points within each component
		{
			// If the point it is not at the local process ...
			if( (parentSystem->checkIfPointProcess(auxs[k]))==false )
			{
				// ...  create it at root system, copying the original point from the remote process
				parentSystem->createPointAtRoot( parentSystem->getPTPoint(auxs[k]) );
				// ...  and add the name of the shared point at the remote process
				parentSystem->addRemoteSharedPoint(auxs[k],parentSystem->getProcNum());
			}
			mauxEntry.first = auxs[k];
			mauxEntry.second = (parentSystem->getPTPointLocal(auxs[k]));
			maux.insert(mauxEntry);
		}
		vLoads[j]->setMPoints(maux);
		maux.clear();
	}
}
*/
// ***

void RigidTetra::setPTPointsCons()
{
	int j, k;
	vector<string> auxs;
	map<string, Point*, less<string> > maux;
	pair<string,Point*> mauxEntry;
	pair<string,int> sharedEntry;
	
	for(j=0; j<vConstraints.size(); j++)		
		//Loop on the Loads
	{
		auxs = (vConstraints[j]->getPointNames());
		for(k=0; k<auxs.size(); k++)		
			// Loop on the names of the points within each component
		{
			/** If the point it is not at the local process ... */
			if( (parentSystem->checkIfPointProcess(auxs[k]))==false )
			{
				/** ...  create it at root system, copying the original point from the remote process */
				parentSystem->createPointAtRoot( parentSystem->getPTPoint(auxs[k]) );
				/** ...  and add the name of the shared point at the remote process */
				parentSystem->addRemoteSharedPoint(auxs[k],parentSystem->getProcNum());
			}
			mauxEntry.first = auxs[k];
			mauxEntry.second = (parentSystem->getPTPointLocal(auxs[k]));
			maux.insert(mauxEntry);
		}
		vConstraints[j]->setMPoints(maux);
		maux.clear();
	}		
}

// ***
/*
void RigidTetra::setPTBodies()
{}
*/
// ***

void RigidTetra::setPPointSurface()
{}

// ***
/*
void RigidTetra::writeFileGraph(ofstream &fGraph, string& gFormat)
{}
*/
// ***

void RigidTetra::writeMSH(ofstream &fGraph)
{}

// ***

void RigidTetra::writeVTU(ofstream &fGraph)
{
	localFGraph << "<?xml version=\"1.0\"?>\n";
	localFGraph << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
	localFGraph << "	<UnstructuredGrid>\n";
	localFGraph << "		<Piece NumberOfPoints=\"" << 0 << "\" NumberOfCells=\"" << 0 << "\">\n";
	localFGraph << "			<Points>\n";
	localFGraph << "				<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "			</Points>\n";
	localFGraph<< "			<Cells>\n";
	localFGraph << "				<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">";
	
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "				<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "				<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "					";

	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph<< "			</Cells>\n";
	
	localFGraph << "		</Piece>\n";
	
	localFGraph << "	</UnstructuredGrid>\n";
	localFGraph << "</VTKFile>\n";
}

// ***

void RigidTetra::writeOOGL(ofstream &fGraph)
{}

// ***
/*
void RigidTetra::writeFileTemp(const double&, ofstream &)
{}
*/
// ***
/*
void RigidTetra::setIniPosJoint(vector<double>& iniValues)
{}
*/